import React, { useState } from 'react';
import {
  MenuFoldOutlined,
  MenuUnfoldOutlined,
  UploadOutlined,
  UserOutlined,
  VideoCameraOutlined,
  LogoutOutlined,
} from '@ant-design/icons';
import { Button, Layout, Menu, theme } from 'antd';
import { useNavigate } from 'react-router-dom'; // 导入 useNavigate
import '../css/main.css'; // 导入自定义的 CSS 文件
import {Outlet} from "react-router-dom";

const { Header, Sider, Content } = Layout;

// 定义退出登录的函数
const handleLogout = (navigate) => {
  console.log('Logging out...');

  // 清除登录状态，例如从 localStorage 中删除 token
  // localStorage.removeItem('token');

  // 跳转到登录页面
  navigate('/login');
};

const Main = () => {
  const [collapsed, setCollapsed] = useState(false);
  const {
    token: { colorBgContainer, borderRadiusLG },
  } = theme.useToken();
  const navigate = useNavigate(); // 创建 navigate 函数
// 定义菜单项点击事件处理器
const handleMenuClick = (e) => {
  console.log('Menu clicked:', e);
  const path = e.item.props.path; // 从菜单项属性中获取路径
  if (path) {
    navigate(path); // 跳转到对应的路径
  }
};
  return (
    <Layout
      style={{
        display: 'flex',
        height: '100vh', // 设置高度为视口高度
      }}
    >
      <Sider
        trigger={null}
        collapsible
        collapsed={collapsed}
        style={{ flex: 1 }} // 设置 flex 属性
      >
        <div className="demo-logo-vertical" />
        <Menu
          theme="dark"
          mode="inline"
          onClick={handleMenuClick} // 添加 onClick 事件处理器
          defaultSelectedKeys={['1']}
          items={[
            {
              key: '1',
              icon: <UserOutlined />,
              label: 'home',
              path: '/home',
            },
            {
              key: '2',
              icon: <VideoCameraOutlined />,
              label: 'user',
              path: '/user',
            },
            
          ]}
        />
      </Sider>
      <Layout style={{ flex: 1 }}> {/* 设置 flex 属性 */}
        <Header
          style={{
            padding: 0,
            background: colorBgContainer,
            display: 'flex', // 设置 header 为 flex 布局
            justifyContent: 'space-between', // 使内容两端对齐
            alignItems: 'center', // 垂直居中
          }}
        >
          <Button
            type="text"
            icon={collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
            onClick={() => setCollapsed(!collapsed)}
            style={{
              fontSize: '16px',
              width: 64,
              height: 64,
            }}
          />
          <Button
            type="text"
            icon={<LogoutOutlined />}
            onClick={() => handleLogout(navigate)} // 绑定退出登录的点击事件
            style={{
              fontSize: '16px',
              marginRight: '20px', // 将按钮放置在右侧
            }}
          />
        </Header>
        <Content
          style={{
            margin: '24px 16px',
            padding: 24,
            background: colorBgContainer,
            borderRadius: borderRadiusLG,
            overflowY: 'auto',
            maxHeight: 'calc(100vh - 128px)',
          }}
        >
          <Outlet/>
        </Content>
      </Layout>
    </Layout>
  );
};

export default Main;